package astoliarskyi.wicket;

import antonst.list.MyArrayList;
import antonst.list.MyList;
import astoliarskyi.wicket.card.Card;
import astoliarskyi.wicket.card.CardBuilder;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.GregorianCalendar;

public enum CoreSystem {
	
	INSTANCE;
	
	private final BigDecimal TRIP_PRICE = new BigDecimal(2);
	
	private final AttemptLogger attemptLogger = new AttemptLogger();
	private final MyList<Wicket> wicketList = new MyArrayList<Wicket>();
	private DataProvider dataProvider;	
	
	public BigDecimal getTripPrice() {
		return TRIP_PRICE;
	}

	public void registerWicket(Wicket wicket) {
		wicketList.add(wicket);
	}

	public void setCurrentDateInAllWickets() {
		Calendar currentDate = Calendar.getInstance();
		for (Wicket wicket : wicketList) {
			wicket.setDate(currentDate);
		}
	}

	public void logAttempt(Card card, boolean decision) {
		attemptLogger.logAttempt(card, decision);
	}

	public AttemptLogger.TotalData getTotalStatistics() {
		return attemptLogger.getTotalStatistics();
	}
	
	public AttemptLogger.DataByType getTotalStatisticsByCardType() {
		return attemptLogger.getTotalStatisticsByCardType();
	}
	
	public void setDataProvider(DataProvider dataProvider) {
		this.dataProvider = dataProvider;
	}

	public Card issueCard(CardBuilder cardBuilder) {
		Card card = cardBuilder.build();
		dataProvider.registerCard(card);
		return card;
	}

	public boolean checkForValidity(Card card) {
		Card registeredCard = dataProvider.getCard(card.getId());
		if (registeredCard != null) {
			return card.equals(registeredCard);
		} else {
			return false;
		}
	}
}
